package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    //  获取容器中ISystemDictionaryService的实现类对象，并注入Bean
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //  查询方法
    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionary:list", "字典目录列表"}, logical = Logical.OR)
    public String list(Model model, QueryObject qo) {
        //  调用Service层的query方法
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        //  将获取到的数据设置到模型中
        model.addAttribute("pageResult", pageInfo);
        //  找视图/WEB-INF/views/systemDictionary/list.ftl
        return "systemDictionary/list";
    }

    //  删除方法
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionary:delete", "字典目录删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        try {
            //  判断id是否存在
            if (id != null) {
                //  调用Service层的deleteById方法
                systemDictionaryService.deleteById(id);
            }
            return new JsonResult(true, "删除成功");
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult(false, "删除失败");
        }
    }

/*    //  去新增或修改页面方法
    @RequestMapping("/input")
    @RequiredPermission(name = "字典目录编辑", expression = "systemDictionary:input")
    public String input(Long id, Model model){
        //  判断id是否存在
        if (id != null) {
            //  调用Service层的selectById方法
            SystemDictionary systemDictionary = systemDictionaryService.selectById(id);
            //  将获取到的数据设置到模型中
            model.addAttribute("systemDictionary", systemDictionary);
        }
        //  找视图/WEB-INF/views/systemDictionary/input.ftl
        return "systemDictionary/input";
    }*/

    //  新增或修改页面方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionary:saveOrUpdate", "字典目录添加/更新"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {
        try {
            //  判断id是否存在
            if (systemDictionary.getId() != null) {
                //  若存在，调用Service层的updateById方法
                systemDictionaryService.updateById(systemDictionary);
            } else {
                //  若不存在，调用Service层的insert方法
                systemDictionaryService.insert(systemDictionary);
            }
            return new JsonResult(true, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
    }
}
